#include "articlemanager.h"
#include <QDebug>

ArticleManager::ArticleManager(QObject *parent) : QObject(parent) {
    creator = new ArticleCreator(this);
    updater = new ArticleUpdater(this);
    lister = new ArticleLister(this);
    fetcher = new ArticleFetcher(this);

    connect(creator, &ArticleCreator::articleCreated, this, &ArticleManager::onArticleCreated);
    connect(updater, &ArticleUpdater::articleUpdated, this, &ArticleManager::onArticleUpdated);
    connect(lister, &ArticleLister::articleListFetched, this, &ArticleManager::onArticleListFetched);
    connect(lister, &ArticleLister::fetchFailed, this, &ArticleManager::onFetchFailed);
    connect(fetcher, &ArticleFetcher::articleFetched, this, &ArticleManager::onArticleFetched);
    connect(fetcher, &ArticleFetcher::fetchFailed, this, &ArticleManager::onFetchFailed);
}

void ArticleManager::setSyncMode(bool sync) {
    creator->setSyncMode(sync);
    updater->setSyncMode(sync);
    lister->setSyncMode(sync);
    fetcher->setSyncMode(sync);
}

void ArticleManager::createArticle(Article article) {
    // 调用 ArticleCreator 创建文章
    creator->createArticle(article.title, article.content, article.images);
}

void ArticleManager::updateArticle(Article article) {
    // 调用 ArticleUpdater 更新文章
    updater->updateArticle(article.article_id, article.title, article.content, article.images);
}

void ArticleManager::fetchArticleList() {
    // 调用 ArticleLister 获取文章
    lister->fetchArticleList();
}

void ArticleManager::fetchArticleById(uint articleID) {
    // 调用 ArticleFetcher 获取特定文章
    fetcher->fetchArticleById(articleID);
}

void ArticleManager::onArticleCreated(bool success, const QString &message) {
    emit articleCreated(success, message);
}

void ArticleManager::onArticleUpdated(bool success, const QString &message) {
    emit articleUpdated(success, message);
}

void ArticleManager::onArticleListFetched(const QList<QJsonObject> &articles) {
    emit articleListFetched(articles);
}

void ArticleManager::onArticleFetched(const QJsonObject &article) {
    emit articleFetched(article);
}

void ArticleManager::onFetchFailed(const QString &error) {
    emit operationFailed(error);
}
